﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class MemberSMSRemindLog
    {
        public int Add(Chain.Model.MemberSMSRemindLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into MemberSMSRemindLog(");
            builder.Append("MemberSMSRemindMemID,MemberSMSRemindMobile,MemberSMSRemindContent,MemberSMSRemindShopID,MemberSMSRemindTime,MemberSMSRemindUserID,MemberSMSRemindAmount,MemberSMSRemindAllAmount,MemberSMSRemindType,MemberSMSRemindBirthday)");
            builder.Append(" values (");
            builder.Append("@MemberSMSRemindMemID,@MemberSMSRemindMobile,@MemberSMSRemindContent,@MemberSMSRemindShopID,@MemberSMSRemindTime,@MemberSMSRemindUserID,@MemberSMSRemindAmount,@MemberSMSRemindAllAmount,@MemberSMSRemindType,@MemberSMSRemindBirthday)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemberSMSRemindMemID", SqlDbType.Int, 4), new SqlParameter("@MemberSMSRemindMobile", SqlDbType.NVarChar, 50), new SqlParameter("@MemberSMSRemindContent", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@MemberSMSRemindShopID", SqlDbType.Int, 4), new SqlParameter("@MemberSMSRemindTime", SqlDbType.DateTime), new SqlParameter("@MemberSMSRemindUserID", SqlDbType.Int, 4), new SqlParameter("@MemberSMSRemindAmount", SqlDbType.Int, 4), new SqlParameter("@MemberSMSRemindAllAmount", SqlDbType.Int, 4), new SqlParameter("@MemberSMSRemindType", SqlDbType.Int, 4), new SqlParameter("@MemberSMSRemindBirthday", SqlDbType.DateTime) };
            cmdParms[0].Value = model.MemberSMSRemindMemID;
            cmdParms[1].Value = model.MemberSMSRemindMobile;
            cmdParms[2].Value = model.MemberSMSRemindContent;
            cmdParms[3].Value = model.MemberSMSRemindShopID;
            cmdParms[4].Value = model.MemberSMSRemindTime;
            cmdParms[5].Value = model.MemberSMSRemindUserID;
            cmdParms[6].Value = model.MemberSMSRemindAmount;
            cmdParms[7].Value = model.MemberSMSRemindAllAmount;
            cmdParms[8].Value = model.MemberSMSRemindType;
            cmdParms[9].Value = model.MemberSMSRemindBirthday;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int MemberSMSRemindID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemberSMSRemindLog ");
            builder.Append(" where MemberSMSRemindID=@MemberSMSRemindID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemberSMSRemindID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MemberSMSRemindID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string MemberSMSRemindIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MemberSMSRemindLog ");
            builder.Append(" where MemberSMSRemindID in (" + MemberSMSRemindIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool DeleteMemByMemID(int MemID)
        {
            string sQLString = "delete from MemberSMSRemindLog where MemberSMSRemindMemID=@MemID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MemID;
            if (DbHelperSQL.ExecuteSql(sQLString, cmdParms) <= 0)
            {
                return false;
            }
            return true;
        }

        public bool Exists(int MemberSMSRemindID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MemberSMSRemindLog");
            builder.Append(" where MemberSMSRemindID=@MemberSMSRemindID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemberSMSRemindID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MemberSMSRemindID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select MemberSMSRemindID,MemberSMSRemindMemID,MemberSMSRemindMobile,MemberSMSRemindContent,MemberSMSRemindShopID,MemberSMSRemindTime,MemberSMSRemindUserID,MemberSMSRemindAmount,MemberSMSRemindAllAmount,MemberSMSRemindType,MemberSMSRemindBirthday ");
            builder.Append(" FROM MemberSMSRemindLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" MemberSMSRemindID,MemberSMSRemindMemID,MemberSMSRemindMobile,MemberSMSRemindContent,MemberSMSRemindShopID,MemberSMSRemindTime,MemberSMSRemindUserID,MemberSMSRemindAmount,MemberSMSRemindAllAmount,MemberSMSRemindType,MemberSMSRemindBirthday ");
            builder.Append(" FROM MemberSMSRemindLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.MemberSMSRemindID desc");
            }
            builder.Append(")AS Row, T.*  from MemberSMSRemindLog T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetMemBirthdayed()
        {
            string sQLString = "select * from MemberSMSRemindLog where datepart(year,MemberSMSRemindTime)=datepart(year,getdate()) and MemberSMSRemindType=1";
            return DbHelperSQL.Query(sQLString);
        }

        public DataSet GetMemPasted()
        {
            string sQLString = "select * from MemberSMSRemindLog where datepart(year,MemberSMSRemindTime)=datepart(year,getdate()) and MemberSMSRemindType=2";
            return DbHelperSQL.Query(sQLString);
        }

        public Chain.Model.MemberSMSRemindLog GetModel(int MemberSMSRemindID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 MemberSMSRemindID,MemberSMSRemindMemID,MemberSMSRemindMobile,MemberSMSRemindContent,MemberSMSRemindShopID,MemberSMSRemindTime,MemberSMSRemindUserID,MemberSMSRemindAmount,MemberSMSRemindAllAmount,MemberSMSRemindType,MemberSMSRemindBirthday from MemberSMSRemindLog ");
            builder.Append(" where MemberSMSRemindID=@MemberSMSRemindID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemberSMSRemindID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MemberSMSRemindID;
            Chain.Model.MemberSMSRemindLog log = new Chain.Model.MemberSMSRemindLog();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["MemberSMSRemindID"] != null) && (set.Tables[0].Rows[0]["MemberSMSRemindID"].ToString() != ""))
            {
                log.MemberSMSRemindID = int.Parse(set.Tables[0].Rows[0]["MemberSMSRemindID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MemberSMSRemindMemID"] != null) && (set.Tables[0].Rows[0]["MemberSMSRemindMemID"].ToString() != ""))
            {
                log.MemberSMSRemindMemID = int.Parse(set.Tables[0].Rows[0]["MemberSMSRemindMemID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MemberSMSRemindMobile"] != null) && (set.Tables[0].Rows[0]["MemberSMSRemindMobile"].ToString() != ""))
            {
                log.MemberSMSRemindMobile = set.Tables[0].Rows[0]["MemberSMSRemindMobile"].ToString();
            }
            if ((set.Tables[0].Rows[0]["MemberSMSRemindContent"] != null) && (set.Tables[0].Rows[0]["MemberSMSRemindContent"].ToString() != ""))
            {
                log.MemberSMSRemindContent = set.Tables[0].Rows[0]["MemberSMSRemindContent"].ToString();
            }
            if ((set.Tables[0].Rows[0]["MemberSMSRemindShopID"] != null) && (set.Tables[0].Rows[0]["MemberSMSRemindShopID"].ToString() != ""))
            {
                log.MemberSMSRemindShopID = int.Parse(set.Tables[0].Rows[0]["MemberSMSRemindShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MemberSMSRemindTime"] != null) && (set.Tables[0].Rows[0]["MemberSMSRemindTime"].ToString() != ""))
            {
                log.MemberSMSRemindTime = DateTime.Parse(set.Tables[0].Rows[0]["MemberSMSRemindTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MemberSMSRemindUserID"] != null) && (set.Tables[0].Rows[0]["MemberSMSRemindUserID"].ToString() != ""))
            {
                log.MemberSMSRemindUserID = int.Parse(set.Tables[0].Rows[0]["MemberSMSRemindUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MemberSMSRemindAmount"] != null) && (set.Tables[0].Rows[0]["MemberSMSRemindAmount"].ToString() != ""))
            {
                log.MemberSMSRemindAmount = int.Parse(set.Tables[0].Rows[0]["MemberSMSRemindAmount"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MemberSMSRemindAllAmount"] != null) && (set.Tables[0].Rows[0]["MemberSMSRemindAllAmount"].ToString() != ""))
            {
                log.MemberSMSRemindAllAmount = int.Parse(set.Tables[0].Rows[0]["MemberSMSRemindAllAmount"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MemberSMSRemindType"] != null) && (set.Tables[0].Rows[0]["MemberSMSRemindType"].ToString() != ""))
            {
                log.MemberSMSRemindType = int.Parse(set.Tables[0].Rows[0]["MemberSMSRemindType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MemberSMSRemindBirthday"] != null) && (set.Tables[0].Rows[0]["MemberSMSRemindBirthday"].ToString() != ""))
            {
                log.MemberSMSRemindBirthday = DateTime.Parse(set.Tables[0].Rows[0]["MemberSMSRemindBirthday"].ToString());
            }
            return log;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM MemberSMSRemindLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.MemberSMSRemindLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update MemberSMSRemindLog set ");
            builder.Append("MemberSMSRemindMemID=@MemberSMSRemindMemID,");
            builder.Append("MemberSMSRemindMobile=@MemberSMSRemindMobile,");
            builder.Append("MemberSMSRemindContent=@MemberSMSRemindContent,");
            builder.Append("MemberSMSRemindShopID=@MemberSMSRemindShopID,");
            builder.Append("MemberSMSRemindTime=@MemberSMSRemindTime,");
            builder.Append("MemberSMSRemindUserID=@MemberSMSRemindUserID,");
            builder.Append("MemberSMSRemindAmount=@MemberSMSRemindAmount,");
            builder.Append("MemberSMSRemindAllAmount=@MemberSMSRemindAllAmount,");
            builder.Append("MemberSMSRemindType=@MemberSMSRemindType,");
            builder.Append("MemberSMSRemindBirthday=@MemberSMSRemindBirthday");
            builder.Append(" where MemberSMSRemindID=@MemberSMSRemindID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MemberSMSRemindMemID", SqlDbType.Int, 4), new SqlParameter("@MemberSMSRemindMobile", SqlDbType.NVarChar, 50), new SqlParameter("@MemberSMSRemindContent", SqlDbType.NVarChar, 0x3e8), new SqlParameter("@MemberSMSRemindShopID", SqlDbType.Int, 4), new SqlParameter("@MemberSMSRemindTime", SqlDbType.DateTime), new SqlParameter("@MemberSMSRemindUserID", SqlDbType.Int, 4), new SqlParameter("@MemberSMSRemindAmount", SqlDbType.Int, 4), new SqlParameter("@MemberSMSRemindAllAmount", SqlDbType.Int, 4), new SqlParameter("@MemberSMSRemindType", SqlDbType.Int, 4), new SqlParameter("@MemberSMSRemindBirthday", SqlDbType.DateTime), new SqlParameter("@MemberSMSRemindID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.MemberSMSRemindMemID;
            cmdParms[1].Value = model.MemberSMSRemindMobile;
            cmdParms[2].Value = model.MemberSMSRemindContent;
            cmdParms[3].Value = model.MemberSMSRemindShopID;
            cmdParms[4].Value = model.MemberSMSRemindTime;
            cmdParms[5].Value = model.MemberSMSRemindUserID;
            cmdParms[6].Value = model.MemberSMSRemindAmount;
            cmdParms[7].Value = model.MemberSMSRemindAllAmount;
            cmdParms[8].Value = model.MemberSMSRemindType;
            cmdParms[9].Value = model.MemberSMSRemindBirthday;
            cmdParms[10].Value = model.MemberSMSRemindID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

